
Yacon.widget.PopupMenu = function(conText)
{
	this.showedSub   = false;	
	Yacon.apply(this,conText);
	this.create();
	
	Yacon.addEvents(document.body,"contextmenu",Yacon.widget.PopupMenu.events.onBodyContextmenu);	
}

Yacon.widget.PopupMenu.Extend(Object,{	
	
	create:function()
	{
		this.menuContainer  = document.createElement("DIV");
		this.menuContainer.className = "menuContainer";
		this.menuContainer.Control    = this;
		if(!window.isIE)
			Yacon.addEvents(this.menuContainer,"mouseout",Yacon.widget.PopupMenu.events.onMenuMouseLeave);
		else
			Yacon.addEvents(this.menuContainer,"mouseleave",Yacon.widget.PopupMenu.events.onMenuMouseLeave);
		
		this.menufrm  = document.createElement("iframe");
		this.menufrm.className   = "menufrm";
		this.menufrm.frameborder = "0";
		this.menufrm.scrolling   = "no";
		
		this.menuDiv  = document.createElement("DIV");
		this.menuDiv.className = "menuDiv";
				
		this.menuTable  = document.createElement("Table");
		this.menuTable.className = "menuTable";
		this.menuTable.border  = "0";
		this.menuTable.cellSpacing = "0";
		this.menuTable.cellPadding = "0";		
		
		this.menuDiv.appendChild(this.menuTable);
		this.menuContainer.appendChild(this.menuDiv);
		this.menuContainer.appendChild(this.menufrm);
		document.body.appendChild(this.menuContainer);	
		
		this.hidden();
	},	
	add:function(key,text,icon,click,subMenu)
	{
		var menuItem    = this.menuTable.insertRow(this.menuTable.rows.length);
			menuItem.menu   = this;			
			menuItem.setAttribute("key",key);
			menuItem.key = key;
			menuItem.disableItem = "false";
			menuItem.className = "menuItem";
		
		if (click)
			menuItem.setAttribute("clickName",click);
		
		var menuItemCell  = menuItem.insertCell(menuItem.cells.length);
		menuItemCell.innerText = "";
		if (text && text.trim().isEqual("-"))
		{
			menuItem.style.height = "5px";
			menuItemCell.style.height = "5px";
			menuItemCell.colSpan = "3";
						
			var spacer  = document.createElement("div");
			spacer.className = "menuSpacer";
			menuItemCell.appendChild(spacer);		
		}else
		{
			menuItem.style.height ="20px";
			menuItemCell.style.height = "20px";	
			menuItemCell.className = "menuTDImg";
			menuItemCell.vAlign = "middle";
			menuItemCell.align = "center";
			menuItemCell.noWrap = "true";
			
			if (icon)
			{		
				if (!icon.contains(contextPath))
					icon = contextPath + icon;
			
				var imgIcon = document.createElement("img");
					imgIcon.className = "menuImg";
					imgIcon.src = icon;
				
				menuItemCell.appendChild(imgIcon);
			}
			
			menuItemCell  = menuItem.insertCell(menuItem.cells.length);		
			menuItemCell.vAlign = "middle";
			menuItemCell.noWrap = "true";
			
			var span = document.createElement("span");
				span.innerHTML        = text;
				span.style.marginLeft = "4px";
				span.style.cursor     = "default";
			menuItemCell.appendChild(span);		
			
			if (menuItemCell.offsetWidth < 150)
				menuItemCell.style.width = "150px";
		}
		
		menuItemCell = menuItem.insertCell(menuItem.cells.length);
		menuItemCell.style.width = "15px";
				
		if (subMenu)
		{
			subMenu.parentMenu = this;
			menuItem.subMenu   = subMenu;
			menuItemCell.innerText ="8";
			menuItemCell.style.font = "8pt Marlett";
		}
		if(!window.isIE)
		{
			Yacon.addEvents(menuItem,"mouseover",Yacon.widget.PopupMenu.events.onItemMouseEnter);
			Yacon.addEvents(menuItem,"mouseout",Yacon.widget.PopupMenu.events.onItemMouseLeave);
		}else
		{
			Yacon.addEvents(menuItem,"mouseenter",Yacon.widget.PopupMenu.events.onItemMouseEnter);
			Yacon.addEvents(menuItem,"mouseleave",Yacon.widget.PopupMenu.events.onItemMouseLeave);
		}
		Yacon.addEvents(menuItem,"click",Yacon.widget.PopupMenu.events.onItemClick);
	},	
	
	show:function(x, y)
	{
		this.setStatus();
		this.hidden();
		
		this.menuDiv.style.left   = x + "px" ;
		this.menuDiv.style.top    = y + "px" ;
		this.menuDiv.style.width  = this.menuTable.offsetWidth + "px" ;
		this.menuDiv.style.height = this.menuTable.offsetHeight+ "px" ;
				
		this.menufrm.style.left   = x + "px" ;
		this.menufrm.style.top    = y + "px" ;
		this.menufrm.style.width  = this.menuDiv.offsetWidth + "px" ;
		this.menufrm.style.height = this.menuDiv.offsetHeight+ "px" ;
		
		
		this.menuContainer.style.left   = x + "px" ;
		this.menuContainer.style.top    = y + "px" ;
		this.menuContainer.style.width  = this.menuDiv.offsetWidth + "px" ;
		this.menuContainer.style.height = this.menuDiv.offsetHeight+ "px" ;
	
		this.menuContainer.style.pixelLeft = Math.min(document.body.offsetWidth - this.menuContainer.offsetWidth, x);
		this.menuContainer.style.pixelTop  = Math.min(document.body.offsetHeight - this.menuContainer.offsetHeight, y);
		this.menuContainer.style.pixelLeft = Math.max(this.menuContainer.style.pixelLeft,0);
		this.menuContainer.style.pixelTop  = Math.max(this.menuContainer.style.pixelTop,0);
		
		this.menuContainer.style.display = "";		
		
		if (this.parentMenu)
			this.parentMenu.showedSub = true;
	},
	setStatus:function()
	{
		var oRow;		
		for(var i=0; i < this.menuTable.rows.length; i++)
		{
			oRow = this.menuTable.rows[i];
			oRow.node = this.node;
			
			if (Yacon.isfireuserEvent("tree_menu_BeforePopup"))
				Yacon.fireUserEvent("tree_menu_BeforePopup",[oRow]);	
		
			if (Yacon.isfireuserEvent("menu_BeforePopup"))
				Yacon.fireUserEvent("menu_BeforePopup",[oRow]);			
			
			oRow.disabled = Yacon.isTrue(oRow.disableItem);
					
			for (var j = 0; j < oRow.cells.length; j++)
			{
				if (oRow.disabled)
					oRow.cells[j].style.color= "#aaaaaa";
				else
					oRow.cells[j].style.color= "";
				
				oRow.cells[j].disabled = oRow.disabled;
				if (!Yacon.isEmpty(oRow.text)&& oRow.cells.length>1)
				{
					oRow.cells[1].children[0].innerHTML = oRow.text;
				}
			}
		}
	},
	hidden:function()
	{
		for(var i = 0; i < this.menuTable.rows.length; i++)
		{
			if(this.menuTable.rows[i].subMenu)
				this.menuTable.rows[i].subMenu.hidden();	
		}
		
		if (this.parentMenu)
			this.parentMenu.showedSub = false;
		
		this.menuContainer.style.display = "none";	
	}

})

Yacon.widget.PopupMenu.events = {	
	onMenuMouseLeave:function()
	{
		var e=window.event || e, target= e.currentTarget,related= e.relatedTarget || e.toElement;
		if(!contains(target, related) && target!==related)
		{
			if (event.srcElement.Control && !event.srcElement.Control.showedSub)
			{
				event.srcElement.Control.hidden();	
			}	
		}		
	},
	onItemMouseEnter:function()
	{
		var e=window.event || e, target= e.currentTarget,related= e.relatedTarget || e.fromElement;
		if(!contains(target, related) && target!==related)
		{
			var meunItem = Yacon.getTR(event.srcElement);		
			if (meunItem.menu.activeItem && meunItem.menu.activeItem.subMenu)
			{
				meunItem.menu.activeItem.subMenu.hidden();
			}		
			meunItem.menu.activeItem  = meunItem;
			meunItem.className = "activeMenuItem";	
			if (meunItem.subMenu)
			{
				meunItem.subMenu.hidden();			
				var abs = Yacon.getAbsPosition(meunItem);				
				meunItem.subMenu.show(abs[0] + meunItem.offsetWidth,abs[1]);
			}		
		}
		
	},
	onItemMouseLeave:function()
	{
		var e=window.event || e, target= e.currentTarget,related= e.relatedTarget || e.toElement;
		if(!contains(target, related) && target!==related)
		{
			var meunItem = Yacon.getTR(event.srcElement);		
			if (meunItem.menu.activeItem)
				meunItem.menu.activeItem.className = "menuItem";
		}
	},
	onItemClick:function()
	{
		var meunItem = Yacon.getTR(event.srcElement);
		if (meunItem.menu.activeItem && !meunItem.menu.activeItem.subMenu && !meunItem.menu.activeItem.disabled)
		{	
			
			meunItem.node = meunItem.menu.node;
			/*外部调用函数*/
			if (Yacon.isfireuserEvent("tree_menu_Click"))
				Yacon.fireUserEvent("tree_menu_Click",[meunItem]);
			
			/*外部调用函数*/
			if (Yacon.isfireuserEvent("menu_Click"))
				Yacon.fireUserEvent("menu_Click",[meunItem]);
			
			/*外部调用函数*/
		
			if (Yacon.isfireuserEvent(meunItem.getAttribute("clickName")))
				Yacon.fireUserEvent(meunItem.getAttribute("clickName"),[meunItem]);	
		}
		
		if (!meunItem.subMenu)
		{	
			meunItem.menu.hidden();
		}
	},
	onBodyContextmenu:function()
	{
		/*IE11 阻止浏览器右键菜单*/
		if(isIE11){
			event.preventDefault();
		}
		
		/*FF 阻止浏览器右键菜单*/
		if(isfirefox){
			event.preventDefault();
		}
			
		event.returnValue=false;
		
	}
}
function contains (a,b){
    try {return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(b)&16)}catch(e){};
}

